package com.wgts.wgts.mvp.view.activity.mycenter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerChangePhoneActivityComponent;
import com.wgts.wgts.dagger.module.ChangePhoneActivityModule;
import com.wgts.wgts.mvp.presenter.IChangePhoneActivityPresenter;
import com.wgts.wgts.mvp.view.IChangePhoneActivityView;
import com.wgts.wgts.netmodle.resp.EditMyCenterMessageEntity;
import com.wgts.wgts.tools.AppInfor;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/2910:50
 *
 */public class ChangePhoneActivity extends BaseActivity implements IChangePhoneActivityView{
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.phone_text)
    TextView phoneText;
    @BindView(R.id.code_et)
    EditText codeEt;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.submit_button)
    RelativeLayout submitButton;

    @Inject
    IChangePhoneActivityPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_center_change_phone_activity);
        ButterKnife.bind(this);
        includeTitle.setText("手机号更换");
        initView();
    }

    @Override
    public void init() {
        DaggerChangePhoneActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .changePhoneActivityModule(new ChangePhoneActivityModule(this))
                .build()
                .inject(this);
    }

    private void initView(){
        phoneText.setText(AppInfor.getInstance().getUserPhone());
    }

    @OnClick({R.id.back_btn_ly, R.id.get_code, R.id.submit_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.get_code:
                sendSms();
                break;
            case R.id.submit_button:
                next();
                break;
        }
    }

    private void sendSms(){
        getCode.setEnabled(false);
        showDialog();
        presenter.sendSms(AppInfor.getInstance().getUserPhone(), "VALIDATE_PHONE");
        presenter.addCountDownTimeListener();
    }

    private void next(){
        if (codeEt.getText().toString() == null || codeEt.getText().toString().equals("")){
            MoHuToast.showToast("请输入验证码");
        }else {
            showDialog();
            presenter.changePhoneResult("UPDATE_USER_PHONE",
                    AppInfor.getInstance().getUserPhone(), codeEt.getText().toString());
        }
    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        MoHuToast.showToast(msg);
    }

    @Override
    public void changePhoneResult() {
        dialogDismiss();
        Intent intent = new Intent(ChangePhoneActivity.this, BindNewPhoneActivity.class);
        startActivity(intent);
    }

    @Override
    public void sendSmsResult() {
        dialogDismiss();
        MoHuToast.showToast("发送成功");
    }

    @Override
    public void countDownTimeOver() {
        getCode.setEnabled(true);
        getCode.setText("重新获取");
        presenter.removeCountDownTimeListener();
    }

    @Override
    public void countDowmTime(final Integer time) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getCode.setText(String.valueOf(time) + "s");
            }
        });
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        presenter.removeCountDownTimeListener();
    }

}
